# coding=utf-8

import simplejson as json
from django.contrib.auth.models import Group
from django.core.urlresolvers import reverse
from django.test import TestCase
from rest_framework.test import APIClient

from account.models import User
from comment.models import Comment
from sms.models import AuthSMS
from utils.key_value import GROUP_NORMAL_USER


def generate_authsms(auth_dict):
    auth = AuthSMS.objects.create(**auth_dict)
    return auth


class CommentTest(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user_data = {
            "username": "13220858781",
            "password": '123456',
        }
        self.user = User.objects.create(username=self.user_data['username'])
        self.user.set_password(self.user_data['password'])
        self.user.phone_number = self.user_data['username']
        self.user.real_name = '姜志鹏'
        self.user.save()
        self.group, ok = Group.objects.get_or_create(name=GROUP_NORMAL_USER)
        self.group.user_set.add(self.user)
        self.comment = Comment.objects.create(source='sms', object_id=1, creater=self.user, comment_to_user=self.user,
                                              content='nnn')
        self.comment.save()
        self.sms_data = {
            "auth_code": "123456",
            "phone_number": "13220858781",
        }
        self.sms = generate_authsms(self.sms_data)

    def _login(self):
        self.client.login(**self.user_data)

    def test_query_all_comments_from_database(self):
        data = {'paging': 'true', 'page': 1, 'limit': '10'}
        res = self.client.get(path=reverse("comment_view"), data=data)
        print res
        self.assertEqual(res.status_code, 200)

    def test_insert_new_comment_into_database(self):
        self._login()
        data = {"content": "做我男朋友把", "object_id": 1, "source": "sms", "comment_to": self.comment.id,
                "comment_to_user": self.user.id}
        res = self.client.post(path=reverse("comment_view"), data=data)
        self.assertEqual(res.status_code, 201)
        content = json.loads(res.content)
        self.assertEqual(content.get("object_id"), data["object_id"])
